package com.example.demo.service;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.mail.MailException;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSenderImpl;
import org.springframework.stereotype.Service;

@Service
public class MailService implements EmailService{
    @Autowired
    private JavaMailSenderImpl javaMailSender;
    @Value("${spring.mail.username}")
    private String from;


//    public void sendSimpleEmail(String to, String subject, String text) {
//        //纯文本邮件信息
//        SimpleMailMessage message = new SimpleMailMessage();
//        message.setFrom(from);
//        message.setTo(to);
//        message.setSubject(subject);
//        message.setText(text);
//        try {
//            javaMailSender.send(message);
//            System.out.println("纯文本发送成功");
//        } catch (MailException e) {
//            System.out.println("纯文本发送失败" + e.getMessage());
//            e.printStackTrace();
//        }
//    }

    @Override
    public void sendSimpleMail(String toEmail, String title, String content) {
        SimpleMailMessage simpleMailMessage = new SimpleMailMessage();
        //设置寄件人
        simpleMailMessage.setFrom(from);
        //设置收件人
        simpleMailMessage.setTo(toEmail);
        //设置标题
        simpleMailMessage.setSubject(title);
        //设置内容
        simpleMailMessage.setText(content);

        //发送邮件
        javaMailSender.send(simpleMailMessage);
    }
}
